package shark.picture.utils.other;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DocxToPdfConverter {
    public static void main(String[] args) {
        String docxPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\送电单3.docx";
        String pdfPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\送电单33.pdf";

        try (FileInputStream fis = new FileInputStream(docxPath);
             XWPFDocument docx = new XWPFDocument(fis);
             FileOutputStream fos = new FileOutputStream(pdfPath)) {

            Document pdfDoc = new Document();
            PdfWriter.getInstance(pdfDoc, fos);
            pdfDoc.open();

            docx.getParagraphs().forEach(paragraph -> {
                try {
                    pdfDoc.add(new Paragraph(paragraph.getText()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            pdfDoc.close();
            System.out.println("DOCX 转 PDF 成功！");
        } catch (IOException | DocumentException e) {
            e.printStackTrace();
            System.out.println("转换失败：" + e.getMessage());
        }
    }
}
